/*Hm9gUKDDwtNjV7Mk8onAzi7R/0YhPp5YSxdJr+KqxdQ/rbWPmbiGYouxMPhl2nBz*/
import { ajax, base, toast, print, cardCache,promptBox } from 'nc-lightapp-front';
import { calculateHeadMoney, delLine, copyLine, pasteLine, pasteToEndLine ,copyInner} from '../../../../public/components/pubUtils/billPubUtil.js';
import { headButton, bodyButton, innerButton } from '../../../../public/components/pubUtils/buttonName';
import {cardBodyControl} from '../../../../public/components/pubUtils/buttonvisible.js';
import { formId, tradeType, dataSource, pkname } from '../constants';
import { getTitle } from '../../../../public/components/getCartTableData.js';
import initTemplate from './initTemplate';
import arapLinkReport from "../../../../public/components/arapBillLinkReport.js";
import {copyBill} from '../../../../public/components/pubUtils/CopyBill/arapCopyBill';
let { getDefData,updateCache ,deleteCacheById} = cardCache;

export default function (props, id) {
  this.dataInSaga.butncode=id;
  switch (id) {
    case headButton.Save://保存
      this.saveBill();
      break;
    case headButton.Add://新增
      //卡片新增时，将浏览态的单据主键缓存，点击取消时重新查询该主键单据
      let pagecode = getDefData('sessionTradeType', dataSource)
      if (!pagecode) {
        pagecode = this.getPagecode();
      }
      //缓存中的交易类型和链接中交易类型不一样的话，重新加载模板
      if(pagecode !=this.getPagecode()){
				props.setUrlParam({ status: 'add', pagecode: pagecode });
				initTemplate.call(this, this.props);	
			}else{
				props.setUrlParam({ status: 'add', pagecode: pagecode });
			}
      this.initAdd();
      break;
    case headButton.Edit://修改
      ajax({
        url: '/nccloud/arap/init/edit.do',
        data: Object.assign({
          pk_bill: this.props.getUrlParam('id'),
          billType: this.billType
        },this.dataInSaga),
        success: (res) => {
          if (res.success) {
            this.props.form.setFormItemsValue(this.formId,{'officialprintuser':{value:null,display:null},'officialprintdate':{display:null,value:null}})
            this.props.cardTable.selectAllRows(this.tableId, false);
            this.props.setUrlParam({ status: 'edit' })
            this.props.cardTable.setStatus(this.tableId, 'edit');
            this.props.form.setFormStatus(this.formId, 'edit');
            this.state.buttonfalg = true;
            this.props.resMetaAfterPkorgEdit();
            this.props.form.setFormItemsDisabled(this.formId, { 'pk_org_v': true });
            this.toggleShow()
          }
        }
      });
      break;
    case headButton.Copy://复制
      copyBill(this,this.getPagecode());
      // ajax({
      //   url: '/nccloud/arap/arappub/copy.do',
      //   data: {
      //     pk_bill: props.getUrlParam('id'),
      //     pageId: this.getPagecode(),
      //     billType: this.billType,
      //     tradeType: this.getPagecode()
      //   },
      //   success: (res) => {
      //     if (res.data) {
      //       this.props.beforeUpdatePage();//打开开关
      //       if (res.data.head) {
      //         this.props.form.setAllFormValue({ [this.formId]: res.data.head[this.formId] });
      //       }
      //       if (res.data.body) {
			// 				this.props.cardTable.setTableData(this.tableId, res.data.body[this.tableId])
      //       }
      //     }
      //     props.setUrlParam({ status: 'add', type: 'copy' })
      //     this.props.cardTable.setStatus(this.tableId, 'edit');
      //     this.props.form.setFormStatus(this.formId, 'edit');
      //     this.state.buttonfalg = true;
      //     this.props.resMetaAfterPkorgEdit();
      //     this.props.form.setFormItemsDisabled(this.formId, { 'pk_org': false });
      //     this.props.updatePage(this.formId, this.tableId);//关闭开关
      //     this.toggleShow()
      //   }
      // });
      break

    case headButton.Delete:
      promptBox({
        color: 'warning',                 
        title: this.state.json['payablebill-000006'],                  /* 国际化处理： 删除*/
        content: this.state.json['payablebill-000010'],             /* 国际化处理： ​确定要删除吗？*/
        noFooter: false,                 
        noCancelBtn: false,              
        beSureBtnName: this.state.json['payablebill-000008'],           /* 国际化处理： 确定*/
        cancelBtnName: this.state.json['payablebill-000009'] ,          /* 国际化处理： 取消*/
        beSureBtnClick: this.delConfirm 
      });
      break
    case headButton.Cancel:
      promptBox({
        color: 'warning',                 
        title: this.state.json['payablebill-000009'],                  /* 国际化处理： 取消*/
        content: this.state.json['payablebill-000007'],             /* 国际化处理： ​确定要取消吗？*/
        noFooter: false,                 
        noCancelBtn: false,              
        beSureBtnName: this.state.json['payablebill-000008'],           /* 国际化处理： 确定*/
        cancelBtnName: this.state.json['payablebill-000009'] ,          /* 国际化处理： 取消*/
        beSureBtnClick: this.cancel  
      });
      break;
    case headButton.Pausetrans://挂起操作
      this.pause('/nccloud/arap/arappub/pause.do');
      break;
    case headButton.Cancelpause://取消挂起操作
      this.pause('/nccloud/arap/arappub/cancelpause.do');
      break;

    case headButton.BillLinkQuery://联查单据
      this.setState({ showBillTrack: true });
      break;
    case headButton.LinkBal://联查余额表
      arapLinkReport(this.props,this.props.form.getFormItemsValue(this.formId, 'pk_payablebill').value,this.billType,this.props.form.getFormItemsValue(this.formId, 'objtype').value);
      break;
    case headButton.LinkDeal://联查处理情况
      ajax({
        url: '/nccloud/arap/arappub/linkdeal.do',
        data: Object.assign({
          pk_bill: this.props.form.getFormItemsValue(this.formId, 'pk_payablebill').value,
          billType: this.billType
        },this.dataInSaga),
        success: (res) => {
          if (res.success) {
            //打开处理情况模态框
            let data = res.data;
            this.Info.combinedExaminationData = data;
            this.handleCombined();
          }
        }
      });
      break;
    case headButton.LinkTerm://联查收付款协议
      let selectedData = this.props.cardTable.getCheckedRows(this.tableId);
      let seletedPks = [];
      if (selectedData.length != 0) {
        selectedData.forEach((val) => {
          seletedPks.push(val.data.values.pk_payableitem.value);
        });
      }
      ajax({
        url: '/nccloud/arap/arappub/linkterm.do',
        data: Object.assign({
          pk_bill: this.props.form.getFormItemsValue(this.formId, 'pk_payablebill').value,
          billType: this.billType,
          pk_items: seletedPks,
          moduleId: '2008'
        },this.dataInSaga),
        success: (res) => {
          if (res.success) {
            //打开收付款协议模态框
            let data = res.data;
            this.Info.selectedPKS = seletedPks;
            this.Info.linkTermData = data;
            this.handleLinkTerm();
          }
        }
      });
      break;
    case headButton.Refresh://刷新
      ajax({
        url: '/nccloud/arap/arappub/cardRefresh.do',
        data: Object.assign({
          pk_bill: this.props.getUrlParam('id'),
          pageId: this.getPagecode(),
          billType: this.billType
        },this.dataInSaga),
        success: (res) => {
          if (res.data) {
            toast({ color: 'success', title: this.state.json['payablebill-000062']});/* 国际化处理： 刷新成功*/
            updateCache(pkname, this.props.getUrlParam('id'), res.data, this.formId, dataSource);
            if (res.data.head) {
              this.props.form.setAllFormValue({ [this.formId]: res.data.head[this.formId] });
            }
            if (res.data.body) {
              this.props.cardTable.setTableData(this.tableId, res.data.body[this.tableId]);
            }
          } else {
            this.props.form.EmptyAllFormValue(this.formId);
            this.props.cardTable.setTableData(this.tableId, { rows: [] });
          }
          this.toggleShow();
        },
        error: (res) => {
          this.props.form.EmptyAllFormValue(this.formId);
          this.props.cardTable.setTableData(this.tableId, { rows: [] });
          deleteCacheById(pkname, this.props.getUrlParam('id'), dataSource);
          this.toggleShow();
          let str = res.message;
          ;
          toast({ color: 'danger', content: str});;
        }
      });
      break;
    case 'InitBuild':
      this.handlePeriodInformation();
      getTitle("close");
      break;
    case 'CancelInitBuild':
      this.handlePeriodInformation();
      getTitle("alerdyClose");
      break;
    case 'Print'://打印
      this.onPrint();
      break;
    case headButton.Output://打印输出
      this.printOutput();
      break;
    case headButton.OfficalPrint://正式打印
      this.officalPrintOutput();
      break;
    case headButton.CancelPrint://取消正式打印
      this.cancelPrintOutput();
      break;
    case headButton.ExportData://导出
      if(this.state.forceRender){
        this.setState({forceRender: false})
      }
      let outbillid = props.getUrlParam('id');
      let pk_bills = [];
      pk_bills.push(outbillid);
      this.Info.selectedPKS = pk_bills;//传递主键数组,之前nc需要导出的加主键
      this.props.modal.show('exportFileModal');//不需要导出的只执行这行代码
      break;
    case headButton.AttachManage://附件管理
      let flag = props.getUrlParam('status');
      if (flag == 'add' || props.getUrlParam('copyFlag') == 'copy' || props.getUrlParam('writebackFlag') == 'redBack') {
        toast({ color: 'warning', content: this.state.json['payablebill-000027'] });/* 国际化处理： 请保存后再进行上传附件！*/
        return;
      }
      this.setState({
        showUploader: true,
        target: null
      })
      break;

    

    //表体肩部的按钮操作
    case bodyButton.AddLine://增行
      if (this.props.form.getFormItemsValue(this.formId, 'pk_org_v').value != null) {
        let rowNum = props.cardTable.getNumberOfRows(this.tableId);
        ajax({
          url: '/nccloud/arap/payablebillpub/addline.do',
          data: this.props.createMasterChildData(this.getPagecode(), this.formId, this.tableId),
          success: (res) => {
            if (res.data) {
              if (res.data.head) {
                this.props.form.setAllFormValue({ [this.formId]: res.data.head[this.formId] });
              }
              if (res.data.body) {
                this.props.cardTable.addRow(this.tableId, rowNum, res.data.body[this.tableId].rows[0].values);
              }
            }
          }
        });
      }
      break;
    case bodyButton.DelLine://删除行
      delLine(this);
      //删行之后控制肩部按钮
      this.onSelected();
      break
    case bodyButton.CopyLine://复制行
       if(copyLine(this, dataSource)){
          this.setState({
            buttonfalg:false
          },()=>{
            cardBodyControl(props,this.state.buttonfalg);
          })
       };

      
      break;
    case bodyButton.PasteToEndLine://粘贴行到末尾
      pasteToEndLine(this, dataSource);
      break;
    case bodyButton.CancelLine://行取消
      this.setState({
        buttonfalg:true
      },()=>{
        cardBodyControl(props,this.state.buttonfalg);
      })   
      //取消之后控制肩部按钮
      this.onSelected();
      break;
    default:
      break
  }
}

/*Hm9gUKDDwtNjV7Mk8onAzi7R/0YhPp5YSxdJr+KqxdQ/rbWPmbiGYouxMPhl2nBz*/